package com.offcn.oauth.config;

import com.offcn.user.feign.UserFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import java.util.ArrayList;

@Configuration
public class MyUserDetailsService implements UserDetailsService {

    @Autowired
    private UserFeign userFeign;

    /**
     *
     * @param username 就是登录时输入的用户名
     * @return
     * @throws UsernameNotFoundException
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        System.out.println("用户输入的用户名是:" + username);
        ArrayList<GrantedAuthority> grantedAuthorities = new ArrayList<>();
        // 用户名随意，密码必须是123456 就可以登录成功
        // 通过用户名 查询用户的密码 将查询到的密码 引入到 第二个参数
        com.offcn.user.pojo.User user = userFeign.findUserByUsername(username).getData();
        return new User(username,user.getPassword(),grantedAuthorities);
    }
}
